package com.base.web.zookeeper.controller;

import com.base.web.core.common.JsonResult;
import com.base.web.zookeeper.model.Property;
import com.base.web.zookeeper.service.ZkService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Des 配置管理
 * Created by wxq on 2018/8/20.
 */
@RestController
@RequestMapping("/property")
public class PropertiesController {

    @Resource
    private ZkService zkService;

    /**
     * 查询配置列表
     * @param map
     * @return
     */
    @GetMapping("/list")
    public JsonResult getList(@RequestParam Map<String, String> map){

        Map<String,String> maps = zkService.queryAllNodeAndData();

        PageInfo<Property> list = zkService.convertMapToModel(maps,map);

        return JsonResult.success(list.getList(),list.getTotal());
    }

    /**
     * 删除配置
     * @param p
     * @return
     */
    @PostMapping("/del")
    public JsonResult del(Property p){

        try {
            zkService.deleteNode(p.getPath());
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }

        return JsonResult.success();
    }

    /**
     * 新增配置
     * @param p
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(Property p){

        try {
            zkService.addNode(p.getPath(),p.getData());
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }

        return JsonResult.success();
    }

    /**
     * 修改配置
     * @param p
     * @return
     */
    @PostMapping("/update")
    public JsonResult update(Property p){

        try {
            zkService.updateNode(p.getPath(),p.getData());
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }

        return JsonResult.success();
    }



}
